package ce.com.cenewbluesdk.entity.k6;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.BaseData;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.ByteUtil;
import ce.com.cenewbluesdk.uitl.Lg;

/**
 * Created by Administrator on 2015/7/3.
 * typedef struct
 * {
 * unsigned char data_type;		//数据类型
 * unsigned char data[xxx];		//该数据类型的具体内容，可参考该类型数据结构体信息
 * }items_info_struct;
 * <p/>
 * typedef struct
 * {
 * unsigned char len;		//整个数据包的长度，不包含其自身
 * unsigned char items;	//属性条目包的个数
 * items_info_struct   info[items];		//条目信息
 * }mix_info_struct;
 * <p/>
 * <p/>
 * 设备侧可组合的数据包DATA_TYPE_DEV_SYNC为：
 * {DATA_TYPE_DEVINFO、DATA_TYPE_BATTERY_INFO、DATA_TYPE_SETTING、DATA_TYPE_ALARM}
 * <p/>
 * APP侧可组合的数据包DATA_TYPE_APP_SYNC为：
 * {DATA_TYPE_USERINFO、DATA_TYPE_SETTING、DATA_TYPE_TIME、DATA_TYPE_WEATHER、DATA_TYPE_ALARM}
 */
public class K6_MixInfoStruct extends BaseData implements Serializable{

    int len;
    byte items;
    List<Property> list = new ArrayList<Property>();

    public List<Property> getList() {
        return list;
    }

    public K6_MixInfoStruct(List<Property> list) {
        this.items = (byte) list.size();
        this.list.addAll(list);
    }

    public byte[] getBytes() {

        int l = 0;
        for (Property p : list) {
            l += p.getBytes().length;
        }
        byte[] bytes = new byte[l + 3];
        System.arraycopy(ByteUtil.int2bytes2(bytes.length-2), 0, bytes, 0, 2);
        bytes[2] = (byte) list.size();

        int index = 3;
        for (Property p : list) {
            System.arraycopy(p.getBytes(), 0, bytes, index, p.getBytes().length);
            index += p.getBytes().length;
        }

        return bytes;
    }

    public static K6_MixInfoStruct parse(byte[] data) {

        int size = data[2] & 0xff;

        Lg.e(" 收到的混合数据的item数量："+size);
        int offset = 3;
        List<Property> list = new ArrayList<Property>();
        for (int i = 0; i < size; i++) {
            int itemLen= ByteUtil.byte2ToInt(new byte[]{ data[offset],data[offset+1]});
            offset=offset+2;
            byte type = data[offset];

            Lg.e(" 收到的混合数据item 类型："+type);

            offset++;
            int len = 0;

            len=itemLen-3;

            byte[] properyData = new byte[len];
            System.arraycopy(data, offset, properyData, 0, properyData.length);
            offset += len;
            Property p = new Property(type, properyData);

            list.add(p);
        }
        K6_MixInfoStruct mix = new K6_MixInfoStruct(list);
        return mix;
    }

    @Override
    public CEDevData toCEDevData() {
        byte[] payload = getBytes();
        CEDevData ceDevData=new CEDevData();
        ceDevData.setCmd(CEBC.K6.CMD_TYPE_SEND);
        ceDevData.setDataType(CEBC.K6.DATA_TYPE_APP_SYNC);
        ceDevData.setData(payload);
        ceDevData.setItemL(payload.length);
        ceDevData.setItemNumber(1);


        return ceDevData;
    }

    public static class Property {
        byte date_type;
        byte[] data;

        public byte getDate_type() {
            return date_type;
        }

        public byte[] getData() {
            return data;
        }

        public Property(byte date_type, byte[] data) {
            this.date_type = date_type;
            this.data = data;
        }

        public byte[] getBytes() {
            byte[] bytes = new byte[data.length + 3];
            System.arraycopy(ByteUtil.int2bytes2(bytes.length), 0, bytes, 0, 2);
            bytes[2] = date_type;
            System.arraycopy(data, 0, bytes, 3, data.length);
            return bytes;
        }

        @Override
        public String toString() {
            return "Property{" +
                    "date_type=" + date_type +
                    ", data=" + Arrays.toString(data) +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "Mix_Info_Struct_K2{" +
                "len=" + len +
                ", items=" + items +
                ", list=" + list +
                '}';
    }
}
